﻿using Magicodes.ExporterAndImporter.Excel.AspNetCore;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.ElectronicSign;
using NP.BPMReportPlatform.EmpCertification.Dto;
using NP.BPMReportPlatform.WordHelper;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.EmpCertification
{
    /// <summary>
    /// 员工证明报表接口
    /// </summary>
    //[Authorize]
    [ApiController]
    [Route("api/empcert")]
    public class EmpCertificationController : BPMReportPlatformController
    {
        private readonly IEmpCertificationService _service;
        readonly IRiskService _riskService;

        public EmpCertificationController(IEmpCertificationService empcertService, IRiskService riskService)
        {
            _service = empcertService;
            _riskService = riskService;
        }

        /// <summary>
        /// 员工证明数据查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetAllAsync")]
        public async Task<PagedResultDto<GetEmpCertResultOutput>> GetAllAsync([FromBody] GetEmpCertResultInput input)
        {
            return await _service.GetAllResult(input);
        }

        /// <summary>
        /// 重推Risk
        /// </summary>
        /// <param name="instanceNumber">流程号</param>
        /// <returns></returns>
        [HttpGet]
        [Route("RepushRisk")]
        public async void RepushRisk([FromQuery] string instanceNumber)
        {
            await _riskService.CreateRisk(instanceNumber);
        }

        /// <summary>
        ///  导出数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("ExportAsync")]
        public async Task<XlsxFileResult<EmpCertExportDto>> ExportAsync([FromBody] GetEmpCertResultInput input)
        {
            input.PageIndex = 1;
            input.PageSize = int.MaxValue;
            var rst = (await _service.GetAllResult(input)).Items.ToList();
            var exportList = ObjectMapper.Map<List<GetEmpCertResultOutput>, List<EmpCertExportDto>>(rst);

            return new XlsxFileResult<EmpCertExportDto>(exportList, "导出Excel");
        }

        /// <summary>
        /// 下载文档（无签署）
        /// </summary>
        /// <param name="instanceNumber"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("download-file-unesign")]
        public async Task<FileResult> DownloadEmpCertificationUnEsign([FromQuery] string instanceNumber)
        {
            var rst = await _service.DownloadEmpCertificationUnEsign(instanceNumber);

            return File(rst, "application/octet-stream", "员工证明文件.docx");
        }
    }
}